package com.ruoyi.common.utils.qnUpload;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.persistent.FileRecorder;
import com.qiniu.util.Auth;

import java.io.IOException;
import java.nio.file.Paths;

/**
 * @ClassName QnUploadUtil
 * @Description 七牛文件上传接口
 * @Author luguanqing
 * @Date 2021/4/16 13:53
 * @Version 1.0
 */
public class QnUploadUtil {

    /**
     * 根据文件路径上传文件信息
     */
    public static void uploadUrl(){
        Configuration cfg = new Configuration(Zone.zone2());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证，然后准备上传
        String accessKey = "SXEPXtZn8Ulw97I1H42LXI2OgeFn6EwEfg0SRbZE";
        String secretKey = "IdvVziX-T0wCHSOnepNOlhu1nN5Bj3-A4wqOCPPd";
        String bucket = "apyjwz";
        //如果是Windows情况下，格式是 D:\\qiniu\\test.png
        String localFilePath = "D:\\sparkle_your_name_am360p.mp4";
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = null;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(localFilePath, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
    }

    /**
     * 断点续上传文件信息
     */
    public static void uploadChunk(){
        Configuration cfg = new Configuration(Zone.zone2());
        //...其他参数参考类注释
        //...生成上传凭证，然后准备上传
        String accessKey = "SXEPXtZn8Ulw97I1H42LXI2OgeFn6EwEfg0SRbZE";
        String secretKey = "IdvVziX-T0wCHSOnepNOlhu1nN5Bj3-A4wqOCPPd";
        String bucket = "apyjwz";
        //如果是Windows情况下，格式是 D:\\qiniu\\test.png
        String localFilePath = "D:\\sparkle_your_name_am360p.mp4";
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = null;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        String localTempDir = Paths.get(System.getenv("java.io.tmpdir"), bucket).toString();
        try {
            //设置断点续传文件进度保存目录
            FileRecorder fileRecorder = new FileRecorder(localTempDir);
            UploadManager uploadManager = new UploadManager(cfg, fileRecorder);
            try {
                Response response = uploadManager.put(localFilePath, key, upToken);
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                System.out.println(putRet.key);
                System.out.println(putRet.hash);
            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println(r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                    //ignore
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        uploadChunk();
    }
}
